/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.easyidea.its.domain.model.system;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.easyidea.its.domain.model.BaseEntity;

/**
 * 
 * @author hukn
 */
@Entity
@Table(name = "Permission")
public class Permission extends BaseEntity {
	private static final long serialVersionUID = 1L;

	private String i18nkey;

	private String code;

	private String dependonCode;

	private String name;

	private List<PermissionAction> permissionActionList;

	private List<Permission> permissionList;

	private List<Role> platformRoleList;
	
	private Permission parentId;

//	private Menu menu;

	private Boolean defaultChecked;

	private int level;


	private Boolean allowUnChecked;

	public Permission() {
	}


	public Permission(Integer id, boolean isDeleted, String code, String name) {
		this.setId(id);
		this.setDeleted(isDeleted);
		this.code = code;
		this.name = name;
	}

	@Column(name = "i18nkey", length = 255)
	public String getI18nkey() {
		return i18nkey;
	}

	public void setI18nkey(String i18nkey) {
		this.i18nkey = i18nkey;
	}

	@Basic(optional = false)
	@Column(name = "code", nullable = false, length = 255)
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	@Column(name = "dependon_code", length = 255)
	public String getDependonCode() {
		return dependonCode;
	}

	public void setDependonCode(String dependonCode) {
		this.dependonCode = dependonCode;
	}

	@Basic(optional = false)
	@Column(name = "name", nullable = false, length = 255)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@OneToMany(mappedBy = "permission")
	public List<PermissionAction> getPermissionActionList() {
		return permissionActionList;
	}

	public void setPermissionActionList(List<PermissionAction> permissionActionList) {
		this.permissionActionList = permissionActionList;
	}

	@OneToMany(mappedBy = "parentId", fetch = FetchType.LAZY)
	public List<Permission> getPermissionList() {
		return permissionList;
	}

	public void setPermissionList(List<Permission> permissionList) {
		this.permissionList = permissionList;
	}

	@JoinColumn(name = "parent_id", referencedColumnName = "id")
	@ManyToOne(fetch = FetchType.LAZY)
	public Permission getParentId() {
		return parentId;
	}

	public void setParentId(Permission parentId) {
		this.parentId = parentId;
	}

//	@ManyToOne
//	@JoinColumn(name = "menu_id", nullable = true)
//	public Menu getMenu() {
//		return menu;
//	}
//
//	public void setMenu(Menu menu) {
//		this.menu = menu;
//	}

	@Column(name = "is_allowunchecked")
	public Boolean getAllowUnChecked() {
		return allowUnChecked;
	}

	public void setAllowUnChecked(Boolean allowUnChecked) {
		this.allowUnChecked = allowUnChecked;
	}

	@Column(name = "is_defaultchecked")
	public Boolean getDefaultChecked() {
		return defaultChecked;
	}

	public void setDefaultChecked(Boolean defaultChecked) {
		this.defaultChecked = defaultChecked;
	}

	@Column(name = "level")
	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	@ManyToMany(fetch = FetchType.LAZY, mappedBy = "permissionList")
	public List<Role> getPlatformRoleList() {
		return platformRoleList;
	}

	public void setPlatformRoleList(List<Role> platformRoleList) {
		this.platformRoleList = platformRoleList;
	}
	/**
	 * Get permission urls
	 * 
	 * @return
	 */
	@Transient
	public List<String> getUrlList() {
		List<String> permissionUrlList = new ArrayList<String>();
		if (permissionActionList != null && permissionActionList.size() > 0) {
			for (PermissionAction permAction : permissionActionList) {
				permissionUrlList.add(permAction.getPackageName());
			}
		}
		return permissionUrlList;
	}
}
